import { createStore } from "vuex";
import axios from "axios";

const store = createStore({
  state() {
    return {
      sellerInfo: {},
      goods: [],
      // 是否展示
      show: false,
      carList: [],
      price: 0,
    };
  },
  getters: {},
  mutations: {
    setSeller(state, payload) {
      state.sellerInfo = payload.data;
    },
    setGoods(state, payload) {
      state.goods = payload.data;
    },
    showCar(state) {
      state.show = !state.show;
    },
    setCarList(state, payload) {
      const hasItem = state.carList.findIndex((item) => {
        return item.name == payload.item.name;
      });

      if (hasItem == "-1") {
        state.carList.push(payload.item);
      }
    },
  },
  actions: {
    async getGoods({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/goods");
      console.log(data);

      commit("setGoods", { data });
    },

    async getSeller({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/seller");
    //   console.log(data);
      // console.log(data.name)
      commit("setSeller", {
        data,
      });
    },
  },
});

export default store;
